package com.beta.cat.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 随机字符串
 * 用于生成唯一标示字符串
 *
 * @author kevin_zhang
 * @date 28/11/2017
 */
public class GuidUtil {

    /**
     * 获取UUID随机字符串
     *
     * @return
     */
    public static String getUuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取32位随机字符串
     *
     * @return
     */
    public static String getGuid() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        return uuid;
    }

    /**
     * 获取指定长度的数字，字母混合字符串
     *
     * @param length
     * @return
     */
    public static String getRandomCharAndStr(int length) {
        String strTmp = "ABCDEFGHJKLMNPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz";
        return getRandomStr(strTmp, length);
    }

    /**
     * 获取指定长度的字母字符串
     *
     * @param length
     * @return
     */
    public static String getRandomCharStr(int length) {
        String strTmp = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        return getRandomStr(strTmp, length);
    }

    /**
     * 获取随机串
     *
     * @param strTmp
     * @param length
     * @return
     */
    private static String getRandomStr(String strTmp, int length) {
        int strLength = strTmp.length();
        Random r = new Random();
        int sIndex;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sIndex = r.nextInt(strLength);
            sb.append(strTmp.charAt(sIndex));
        }
        return sb.toString();
    }

    /**
     * 获取指定长度的数字字符串
     * 非0开头的数字
     *
     * @param length
     * @return
     */
    public static String getRandomRealNum(int length) {
        return getRandomNumStr(1, length);
    }

    /**
     * 获取指定长度的数字字符串
     *
     * @param length
     * @return
     */
    public static String getRandomNum(int length) {
        return getRandomNumStr(0, length);
    }

    /**
     * 获取指定长度的数字字符串
     *
     * @param isZeroStart
     * @param length
     * @return
     */
    private static String getRandomNumStr(int isZeroStart, int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int num = random.nextInt(10);
            if (i == 0 && isZeroStart == 1 && num == 0) {
                num += random.nextInt(8) + 1;
            }
            sb.append(num);
        }
        return sb.toString();
    }

    /**
     * 生成订单号
     *
     * @return
     */
    public static String getOrderSrl() {
        String timeStr = DateUtil.currentDate(DateUtil.YYYYMMDDHHMMSSSSS);
        return timeStr + getRandomNum(4);
    }
}
